package com.syt.sms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.syt.sms.service.SMSService;
import com.syt.sms.utils.ConstantPropertiesUtils;
import com.syt.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xxx
 * @Date: 2023/2/24 19:51
 * @Description: 手机短信
 */
@Service
public class SMSServiceImpl implements SMSService {


    //rabbitmq 发送手机验证码
    @Override
    public boolean sendPhoneCode(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())){
            //String code = (String) msmVo.getParam().get("code");
            boolean isSend = this.sendCodeByPhone(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }



    /**
     * 则生成六位验证码，通过手机短信服务进行发送 2
     * @param phone
     * @param param
     * @return
     */

    private boolean sendCodeByPhone(String phone,Map<String,Object> param) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }
        //手机号码不为空，整合阿里云短信服务
        DefaultProfile profile = DefaultProfile.getProfile(ConstantPropertiesUtils.REGION_ID,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SERVICE);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        // 设置发送相关的参数
        request.putQueryParameter("PhoneNumbers", phone); // 手机号
        request.putQueryParameter("SignName", "吴峰的在线教育"); // 之前申请的阿里云签名的名称
        request.putQueryParameter("TemplateCode", "SMS_264950333"); // 之前申请的阿里云模板的模板code


        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); // 验证码数据，需要转换成json数据传递，这也是为什么在controller中要将验

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }




    /**
     * 则生成六位验证码，通过手机短信服务进行发送 1
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean sendCodeByPhone(String phone, String code) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }
        //手机号码不为空，整合阿里云短信服务
        DefaultProfile profile = DefaultProfile.getProfile(ConstantPropertiesUtils.REGION_ID,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SERVICE);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        // 设置发送相关的参数
        request.putQueryParameter("PhoneNumbers", phone); // 手机号
        request.putQueryParameter("SignName", "吴峰的在线教育"); // 之前申请的阿里云签名的名称
        request.putQueryParameter("TemplateCode", "SMS_264950333"); // 之前申请的阿里云模板的模板code
        Map<String,Object> param=new HashMap<>();
        param.put("code", code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); // 验证码数据，需要转换成json数据传递，这也是为什么在controller中要将验

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
